﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DataControls;
namespace GiaoVien_QuanLy_DeTai
{
    public partial class HOCVI_Form : Form
    {
        
        HOCVI hv = new HOCVI();
        HOCVI_Data hocvi_Data = new HOCVI_Data();
        public HOCVI_Form()
        {
            InitializeComponent();
        }

        private void HOCVI_Form_Load(object sender, EventArgs e)
        {
            btnThem.Visible = true;
            btnSua.Visible = true;
            btnXoa.Visible = true;
            txtHeSoHV.Text = "";
            txtTenHocVi.Text = "";
            txtMaHocVi.Text = "HV";
            
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            hv.hienthi_HocVi(dtgHOCVI);
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaHocVi.Text == "")
            {
                txtMaHocVi.Focus();
                MessageBox.Show("Ban chua dien ma so hoc vi");
                return;
            }
            else if (txtTenHocVi.Text == "")
            {
                txtTenHocVi.Focus();
                MessageBox.Show("Ban chua dien ten hoc vi");
                return;
            }
            else if (txtHeSoHV.Text == "")
            {
                txtHeSoHV.Focus();
                MessageBox.Show("Ban chua he so hoc vi");
                return;
            }
            else if (hocvi_Data.kiemtra_MaHocVi(txtMaHocVi.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung Ma Hoc Vi");
                txtMaHocVi.Focus();
                return;
            }
            else if (hocvi_Data.kiemtra_TenHocVi(txtTenHocVi.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung Ten Hoc Vi");
                txtTenHocVi.Focus();
                return;
            }
            else
            {

                hv.maHocVi = (txtMaHocVi.Text).ToString();
                hv.tenHocVi = (txtTenHocVi.Text).ToString();
                try
                {
                    if ((hv.hesoHV = (float)(Convert.ToDouble(txtHeSoHV.Text))) <= 0)
                    {
                        MessageBox.Show(" He so hoc vi chua hop le ");
                        txtHeSoHV.Focus();
                        return;
                    };
                    hv.them_HocVi(hv);
                    hv.hienthi_HocVi(dtgHOCVI);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("He so hoc vi chua hop le  \n" + ex.Message);
                    txtHeSoHV.Focus();
                    return;
                }
                txtHeSoHV.Text = "";
                txtTenHocVi.Text = "";
                txtMaHocVi.Text = "HV";
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {             
            if (txtMaHocVi.Text == "")
            {
                txtMaHocVi.Focus();
                MessageBox.Show("Ban chua dien ma hoc vi");
                return;
            }
            else if (hocvi_Data.kiemtra_MaHocVi(txtMaHocVi.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa hoc vi trong Giao Vien");
                return;
            }
            else
            {
                hv.xoa_HocVi(txtMaHocVi.Text.ToString());
                hv.hienthi_HocVi(dtgHOCVI);
                txtHeSoHV.Text = "";
                txtTenHocVi.Text = "";
                txtMaHocVi.Text = "HV";
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaHocVi.Text == "")
            {
                txtMaHocVi.Focus();
                MessageBox.Show("Ban chua dien ma so hoc vi");
                return;
            }
            else if (txtTenHocVi.Text == "")
            {
                txtTenHocVi.Focus();
                MessageBox.Show("Ban chua dien ten hoc vi");
                return;
            }
            else if (txtHeSoHV.Text == "")
            {
                txtHeSoHV.Focus();
                MessageBox.Show("Ban chua he so hoc vi");
                return;
            }
            else
            {
                hv.maHocVi = (txtMaHocVi.Text).ToString();
                hv.tenHocVi = (txtTenHocVi.Text).ToString();
                try
                {
                    if ((hv.hesoHV = (float)(Convert.ToDouble(txtHeSoHV.Text))) <= 0)
                    {
                        MessageBox.Show(" He so hoc vi chua hop le ");
                        txtHeSoHV.Focus();
                        return;
                    };
                    hv.sua_HocVi(hv);
                    hv.hienthi_HocVi(dtgHOCVI);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("He so hoc vi chua hop le \n" + ex.Message);
                    txtHeSoHV.Focus();
                    return;
                }
                txtHeSoHV.Text = "";
                txtTenHocVi.Text = "";
                txtMaHocVi.Text = "HV";
            }
        }

        private void dtgHOCVI_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dtgHOCVI.AllowUserToAddRows = false;
            int dong = e.RowIndex;
            txtMaHocVi.Text = dtgHOCVI.Rows[dong].Cells[0].Value.ToString();
            txtTenHocVi.Text = dtgHOCVI.Rows[dong].Cells[1].Value.ToString();
            txtHeSoHV.Text = dtgHOCVI.Rows[dong].Cells[2].Value.ToString();
            
        }

        private void txtMaHocVi_MouseClick(object sender, MouseEventArgs e)
        {
            txtHeSoHV.Text = "";
            txtTenHocVi.Text = "";
            txtMaHocVi.Text = "HV";
        }
       
    }
}
